﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PopShops.Entities
{
    public class ProductQuery : Query
    {
        public List<String> IncludeKeywords { get; set; }
        public List<String> ExcludeKeywords { get; set; }
        public int? MerchantID { get; set; }
        public int? MerchantTypeID { get; set; }
        public int? BrandID { get; set; }
        public double? PriceMin { get; set; }
        public double? PriceMax { get; set; }
        public ProductSort SortBy { get; set; }
        public int? PageSize { get; set; }
        public int? PageNumber { get; set; }
        public bool? IncludeProductIDs { get; set; }
        public long? ProductID { get; set; }
        public bool? IncludeProductGroup { get; set; }
        public long? ProductGroupID { get; set; }
        public bool? IncludeDeals { get; set; }
        public string SubID { get; set; }

        public ProductQuery()
            : base(QueryMethod.Products, "products", typeof(SearchResults))
        {
            //Deafault SortBy
            SortBy = ProductSort.none;
            IncludeKeywords = new List<string>();
            ExcludeKeywords = new List<string>();

            //Catalog Key
            base.filters.Add("catalog_key", PsContext.Current.CatalogKey);
        }


        public override void GenerateParams()
        {
            string param = "";

            //Keywords
            if (IncludeKeywords.Count > 0)
            {
                foreach (String s in IncludeKeywords)
                    param += HttpUtility.UrlEncode(s) + "+";
                param = param.Substring(0, param.Length - 1);
            }

            if (ExcludeKeywords.Count > 0)
            {
                foreach (String s in ExcludeKeywords)
                    param += " -" + HttpUtility.UrlEncode(s);
            }

            if (!string.IsNullOrEmpty(param))
                base.filters.Add("keywords", param);

            //merchant_id
            if (MerchantID.HasValue)
                base.filters.Add("merchant_id", MerchantID.Value.ToString());

            //merchant_type_id
            if (MerchantTypeID.HasValue)
                base.filters.Add("merchant_type_id", MerchantTypeID.Value.ToString());

            //brand_id
            if (BrandID.HasValue)
                base.filters.Add("brand_id", BrandID.Value.ToString());

            //price_min
            if (PriceMin.HasValue)
                base.filters.Add("price_min", PriceMin.Value.ToString());

            //price_max
            if (PriceMax.HasValue)
                base.filters.Add("price_max", PriceMax.Value.ToString());

            //product_sort
            if (SortBy != ProductSort.none)
                base.filters.Add("product_sort", SortBy.ToString());

            //product_limit
            if (PageSize.HasValue)
                base.filters.Add("product_limit", PageSize.Value.ToString());

            //product_offset
            if (PageNumber.HasValue)
                base.filters.Add("product_offset", PageNumber.Value.ToString());

            //include_product_ids
            if (IncludeProductIDs.HasValue)
                base.filters.Add("include_product_ids", Convert.ToInt32(IncludeProductIDs.Value).ToString());

            //product_id
            if (ProductID.HasValue)
                base.filters.Add("product_id", ProductID.Value.ToString());

            //include_product_groups
            if (IncludeProductGroup.HasValue)
                base.filters.Add("include_product_groups", Convert.ToInt32(IncludeProductGroup.Value).ToString());

            //product_group_id
            if (ProductGroupID.HasValue)
                base.filters.Add("product_group_id", ProductGroupID.Value.ToString());

            //include_deals
            if (IncludeDeals.HasValue)
                base.filters.Add("include_deals", Convert.ToInt32(IncludeDeals.Value).ToString());

            //url_subid
            if (!string.IsNullOrEmpty(SubID))
                base.filters.Add("url_subid", SubID);
        }
    }
}
